﻿package com.ouken.activity;

import java.util.ArrayList;

import android.content.Context;

import com.ouken.tool.Device;
import com.ouken.tool.DeviceslistAdapter;

import device_ctr.Device_ctr;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SearchView;
import android.widget.Toast;

public class DevicesListActivity extends Activity {
	private Context context;
	private ListView available_device_listView;
	private ListView unavailable_device_listView;
	private RadioButton line_on_radioButton;
	private RadioButton line_off_radioButton;
	private Boolean isCheck_line_on_radioButton;
	private Boolean isCheck_line_off_radioButton;
	private ViewGroup.LayoutParams params;
	private ArrayList<Device> OnlineDeviceLists;
	private ArrayList<Device> OfflineDeviceLists;
	private String[] contents ;/* {"QZ0112345678","银色那台车","4","1",
			                     "QZ0112345678","大哥的小黑","1","1",
			                     "QZ0212345678","同天窗的車庫","2","1",
			                     "QZ0412345678","老幾的防盜系統","3","1",
			                     "QZ0712345678","小林家的對講機","4","1",
			                     "QZ0881345678","银色那台车","4","1",
			                     "QZ0312345678","银色那台车","4","1",
			                     "QZ0512345678","银色那台车","4","1",
			                     "QZ1012345678","银色那台车","4","1",
			                     "QZ0612345678","银色那台车","4","1",
			                     "QZ0912345678","银色那台车","4","1",
			                     "QZ0212345678","银色那台车","4","0",
			                     "QZ0312345678","银色那台车","4","0"}*/
	private String result = ("QZ0112345670,银色那台车,1,1,"
            +"QZ0112345671,大哥的小黑,4,1,"
            +"QZ0212345672,同天窗的車庫,3,1,"
            +"QZ0412345673,老幾的防盜系統,2,1,"
            +"QZ0712345674,小林家的對講機,3,1,"
            +"QZ0881345675,門禁,2,1,"
            +"QZ0312345676,摩托車,2,1,"
            +"QZ0512345677,小檯燈,1,1,"
            +"QZ1012345678,遙控門,2,1,"
            +"QZ0612345679,開關,3,1,"
            +"QZ0912345661,鑰匙,2,1,"
            +"QZ0212345662,银色那台车,5,0,"
            +"QZ0312345663,银色那台车,5,0");
	private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
         public void run () {
        	 updateDeviceList();
        	 handler.postDelayed(this,2500);
         }
    };
	private void initView(){
		context = this;
		available_device_listView = (ListView)findViewById(R.id.available_device_listView);
		unavailable_device_listView = (ListView)findViewById(R.id.unavailable_device_listView);
		line_on_radioButton = (RadioButton)findViewById(R.id.line_on_button);
		line_off_radioButton = (RadioButton)findViewById(R.id.line_off_button);
		params = available_device_listView.getLayoutParams();
		OnlineDeviceLists = new ArrayList<Device>();
		OfflineDeviceLists = new ArrayList<Device>();
		
		isCheck_line_on_radioButton = true;
		isCheck_line_off_radioButton = false;
		
	}
	
	private void updateDeviceList(){
		int dataLength;
		Device deviceTmp;
		
		Device_ctr kk=new Device_ctr(context);
		result=kk.Refresh();
		
		if(result == null) return;
		//Log.e("S", "Refesh list");
		OnlineDeviceLists.clear();
		OfflineDeviceLists.clear();
		
		contents = result.split(",");
		dataLength = contents.length/5;
		
		for(int i =0; i<dataLength; i++){
		  deviceTmp = new Device(contents[i*5],contents[i*5+1],Integer.parseInt(contents[i*5+2]),contents[i*5+3],contents[i*5+4]);
		  if(deviceTmp.getIsOnline()) OnlineDeviceLists.add(deviceTmp);
		  else OfflineDeviceLists.add(deviceTmp);
		}
		
		DeviceslistAdapter available_adapter=new DeviceslistAdapter(this,OnlineDeviceLists);  
		available_device_listView.setAdapter(available_adapter);
		
		DeviceslistAdapter unavailable_adapter=new DeviceslistAdapter(this,OfflineDeviceLists);  
		unavailable_device_listView.setAdapter(unavailable_adapter);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device_list);
		
		initView();
		
		updateDeviceList();
		//*******************************************************
		handler.removeCallbacks(runnable);
		handler.postDelayed(runnable,2500);
		//*******************************************************
		available_device_listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(android.widget.AdapterView<?> arg0,
					android.view.View arg1, int arg2, long arg3) {
				Intent intent ;
				if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_01){
					 intent = new Intent(context, Device01ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_02){
					 intent = new Intent(context, Device02ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_03){
					 intent = new Intent(context, Device03ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_04){
					 intent = new Intent(context, Device04ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_05){
					 intent = new Intent(context, Device05ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_06){
					 intent = new Intent(context, Device06ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_07){
					 intent = new Intent(context, Device07ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_08){
					 intent = new Intent(context, Device08ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_09){
					 intent = new Intent(context, Device09ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_10){
					 intent = new Intent(context, Device10ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_11){
					 intent = new Intent(context, Device11ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_12){
					 intent = new Intent(context, Device12ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_13){
					 intent = new Intent(context, Device13ControlActivity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_14){
					 intent = new Intent(context, Device14ControlActivity.class); 
				}else return;
				Device_ctr jj = new Device_ctr(context);
				//jj.AddWifiInfo(OnlineDeviceLists.get(arg2).getDeviceSSID().toString(),"12345678",3);
				jj.Connect_wifi(OnlineDeviceLists.get(arg2).getDeviceSSID().toString());
				intent.putExtra("deviceSSID",OnlineDeviceLists.get(arg2).getDeviceSSID().toString());
		        intent.putExtra("description",OnlineDeviceLists.get(arg2).getDeviceDescription());
		        intent.putExtra("deviceSignal",OnlineDeviceLists.get(arg2).getDeviceSignal());
		        intent.putExtra("devicePWD",OnlineDeviceLists.get(arg2).getDevicePWD().toString());
		        
		        startActivity(intent);
			}
		});
		
		line_on_radioButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!isCheck_line_on_radioButton){
					line_off_radioButton.setChecked(false);
					line_on_radioButton.setChecked(true);
					
					isCheck_line_on_radioButton =!isCheck_line_on_radioButton;
					isCheck_line_off_radioButton =!isCheck_line_off_radioButton;
					
					params.height = 428;
					available_device_listView.setLayoutParams(params);
				}
			}
		});
		
		
		line_off_radioButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!isCheck_line_off_radioButton){
					line_on_radioButton.setChecked(false);
					line_off_radioButton.setChecked(true);
					isCheck_line_on_radioButton =!isCheck_line_on_radioButton;
					isCheck_line_off_radioButton =!isCheck_line_off_radioButton;
					params.height = 0;
					available_device_listView.setLayoutParams(params);
				}
			}
		});
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_MENU) {
			Intent intent = new Intent(context, MySettingsActivity.class); 
	        startActivity(intent);
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//menu.add(0, 1, 1, R.string.action_settings);
		//Intent intent = new Intent(context, MySettingsActivity.class); 
        //startActivity(intent);
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == 1){
        	Intent intent = new Intent(context, MySettingsActivity.class); 
	        startActivity(intent);
        }
        return true;
    }
}
